package top.mcwebsite.playandroid.business.me.impl.coin.ui

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.rounded.HelpOutline
import androidx.compose.material.icons.rounded.Sort
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Param
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.business.me.impl.coin.vm.CoinIntent
import top.mcwebsite.playandroid.business.me.impl.coin.vm.CoinUiEffect
import top.mcwebsite.playandroid.business.me.impl.coin.vm.CoinViewModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.common.user.api.model.CoinInfo
import top.mcwebsite.playandroid.strings.Str

@Route(RouteConfig.Coin.PATH)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CoinPage(
    @Param(RouteConfig.Coin.Param.COIN_INFO) coinInfo: CoinInfo
) {
    val viewModel: CoinViewModel = viewModel()
    val uiState by viewModel.uiStateFlow.collectAsState()
    viewModel.HandleUiEffect {
        when (it) {
            is CoinUiEffect.NavigationBackUiEffect -> {
                EasyRouter.popBackStack()
            }
            is CoinUiEffect.NavigationToCoinRankPageUiEffect -> {
                EasyRouter.navigate(RouteConfig.CoinRank.PATH)
            }
        }
    }
    LaunchedEffect(key1 = Unit) {
        viewModel.sendIntent(CoinIntent.InitPageIntent(coinInfo))
    }
    Scaffold(
        topBar = {
            TopAppBar(
                navigationIcon = {
                    IconButton(onClick = {
                        viewModel.sendIntent(CoinIntent.BackClickIntent)
                    }) {
                        Icon(Icons.Default.ArrowBack, contentDescription = stringResource(id = Str.back))
                    }
                },
                title = {
                    Text(text = stringResource(id = Str.my_coin))
                },
                actions = {
                    IconButton(onClick = { viewModel.sendIntent(CoinIntent.CoinRuleDialogVisibleChangeIntent(true)) }) {
                        Icon(imageVector = Icons.Rounded.HelpOutline, contentDescription = null)
                    }
                    IconButton(onClick = { viewModel.sendIntent(CoinIntent.ClickRankIntent) }) {
                        Icon(imageVector = Icons.Rounded.Sort, contentDescription = null)
                    }
                }
            )
        }
    ) {
        Column(modifier = Modifier.padding(it)) {
            CoinHeader(coinCount = uiState.coinCount)
            CoinContent(lazyPagingItems = viewModel.pagerFlow.collectAsLazyPagingItems())
            CoinRuleDialog(isShow = uiState.coinRuleDialogVisible) {
                viewModel.sendIntent(CoinIntent.CoinRuleDialogVisibleChangeIntent(false))
            }
        }
    }
}
